#!/bin/sh
#
# Salt minion
###################################

# LSB header

### BEGIN INIT INFO
# Provides:          salt-minion
# Required-Start:    $local_fs $remote_fs $network $named $time
# Should-Start:      $time ypbind smtp
# Required-Stop:	 $local_fs $remote_fs $network $named $time
# Should-Stop:       ypbind smtp
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Salt minion daemon
# Description:       This is the Salt minion daemon that can be controlled by the
#                    Salt master.
### END INIT INFO


# chkconfig header

# chkconfig: 345 97 04
# description:  This is the Salt minion daemon that can be controlled by the Salt master.
#
# processname: /usr/bin/salt-minion


DEBIAN_VERSION=/etc/debian_version
SUSE_RELEASE=/etc/SuSE-release
# Source function library.
if [ -f $DEBIAN_VERSION ]; then
   break
elif [ -f $SUSE_RELEASE -a -r /etc/rc.status ]; then
    . /etc/rc.status
else
    . /etc/rc.d/init.d/functions
fi

# Default values (can be overridden below)
SALTMINION=/usr/bin/salt-minion
PYTHON=/usr/bin/python
MINION_ARGS=""

if [ -f /etc/default/salt ]; then
    . /etc/default/salt
fi

SERVICE=salt-minion
PROCESS=salt-minion

RETVAL=0
WATCHDOG_CRON="/etc/cron.d/salt-minion"

set_watchdog() {
    if [ ! -f $WATCHDOG_CRON ]; then
        echo -e '* * * * * root /usr/bin/salt-daemon-watcher --with-init\n' > $WATCHDOG_CRON
        # Kick the watcher for 1 minute immediately, because cron will wake up only afterwards
        /usr/bin/salt-daemon-watcher --with-init & disown
    fi
}

remove_watchdog() {
    rm $WATCHDOG_CRON 2>/dev/null || true
    kill -9 $(ps uax | grep [s]alt-daemon-watcher | awk '{print $2}') 2>/dev/null
}

start() {
    set_watchdog;
    echo -n $"Starting salt-minion daemon: "
    if [ -f $SUSE_RELEASE ]; then
        startproc -p /var/run/$SERVICE.pid $SALTMINION -d $MINION_ARGS
        rc_status -v
        RETVAL=$?
    elif [ -e $DEBIAN_VERSION ]; then
        if [ -f $LOCKFILE ]; then
            echo -n "already started, lock file found"
            RETVAL=1
        elif $PYTHON $SALTMINION -d $MINION_ARGS >& /dev/null; then
            echo -n "OK"
            RETVAL=0
        fi
    else
        if [[ $(pidofproc $PROCESS) ]]; then
	          RETVAL=$?
	          echo -n "already running"
        else
	          daemon --check $SERVICE $SALTMINION -d $MINION_ARGS
	          RETVAL=$?
        fi
    fi
    echo
    return $RETVAL
}

stop() {
    IS_RESTARTING=$1
    if [ -z $IS_RESTARTING ]; then
        remove_watchdog;
    fi
    echo -n $"Stopping salt-minion daemon: "
    if [ -f $SUSE_RELEASE ]; then
        killproc -TERM $SALTMINION
        rc_status -v
    elif [ -f $DEBIAN_VERSION ]; then
        # Added this since Debian's start-stop-daemon doesn't support spawned processes
        if ps -ef | grep "$PYTHON $SALTMINION" | grep -v grep | awk '{print $2}' | xargs kill &> /dev/null; then
            echo -n "OK"
            RETVAL=0
        else
            echo -n "Daemon is not started"
            RETVAL=1
        fi
    else
        killproc $PROCESS
    fi
    RETVAL=$?
    echo
}

restart() {
   stop 1;
   start;
}

# See how we were called.
case "$1" in
    start|stop|restart)
        $1
        ;;
    status)
        if [ -f $SUSE_RELEASE ]; then
            echo -n "Checking for service salt-minion "
            checkproc $SALTMINION
            rc_status -v
            RETVAL=$?
        elif [ -f $DEBIAN_VERSION ]; then
            if [ -f $LOCKFILE ]; then
                RETVAL=0
                echo "salt-minion is running."
            else
                RETVAL=1
                echo "salt-minion is stopped."
            fi
        else
            status $PROCESS
            RETVAL=$?
        fi
        ;;
    condrestart|try-restart)
        [ -f $LOCKFILE ] && restart || :
        ;;
    reload)
        echo "can't reload configuration, you have to restart it"
        RETVAL=1
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload}"
        exit 1
        ;;
esac
exit $RETVAL
